/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import business.UserManager;
import entity.Event;
import entity.Visibility;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 * This is the bean managing user pages (different from home page)
 *
 * @author dami
 */
@ManagedBean
@SessionScoped
public class userDataBean implements Serializable {

    //The model of the calendar
    private ScheduleModel eventModel;
    //The event in which we save the event selected by the user.
    private ScheduleEvent selectedEvent = new DefaultScheduleEvent();

    private List<Event> userCalendar;
    //The String containing the email of the user received as parameter from the page.
    private String email;
    //A boolean saying if the user has a private calendar or not.
    boolean privateCal = false;

    @EJB
    private UserManager um;

    /**
     * load the list of te events the user is participating in We have to Hide
     * details of the private ones.
     */
    public void init() {
        userCalendar = um.loadUserCalendar(email);
        if (userCalendar == null) {
            privateCal = true;
        } else {
            privateCal = false;
            //if the user a public calendar, we load the events in the eventModel.
            eventModel = new DefaultScheduleModel();
            for (Event e : userCalendar) {
                if (!e.isDeleted()) {
                    if (e.getVisibiltiy().equals(Visibility.PRIVATE)) {
                        //if the event is private, we hide the details, so we don't load the title but only the starting and ending times.
                        eventModel.addEvent(new DefaultScheduleEvent("*PRIVATE EVENT*", e.getStartDate(), e.getEndDate(), e));
                    } else {
                        eventModel.addEvent(new DefaultScheduleEvent(e.getTitle(), e.getStartDate(), e.getEndDate(), e));
                    }
                }
            }
        }
    }

    public ScheduleModel getEventModel() {
        return eventModel;
    }

    public void setEventModel(ScheduleModel eventModel) {
        this.eventModel = eventModel;
    }

    public ScheduleEvent getSelectedEvent() {
        return selectedEvent;
    }

    public void setSelectedEvent(ScheduleEvent selectedEvent) {
        this.selectedEvent = selectedEvent;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isPrivateCal() {
        return privateCal;
    }

    public void setPrivateCal(boolean privateCal) {
        this.privateCal = privateCal;
    }

    public void onEventSelect(SelectEvent selectEvent) {
        selectedEvent = (ScheduleEvent) selectEvent.getObject();
    }

    /**
     * The function redirecting to the event:
     *
     * @return
     */
    public String redirectToEvent() {
        Event e = (Event) selectedEvent.getData();
        return "eventPage?faces-redirect=true&idEv=" + e.getId();
    }

    /**
     * This functions, when a user clicks on an email to see the user page,
     * checks if the email is my one. in that case, redirects you to the home
     * page. Else it redirects you to the corresponding userPage.
     *
     * @param email
     * @return the string representing the correct page
     */
    public String redirectToUserPage(String email) {
        if (um.getLoggedUser().getEmail().equals(email)) {
            return "home?faces-redirect=true";
        } else {
            return "userPage?faces-redirect=true&email=" + email;
        }
    }

    /**
     * the function checking if the event selected on the schedule is a private
     * one. this function allow to prevent of showing details of a private event
     *
     * @return true if the event is private
     */
    public boolean checkPrivate() {
        Event e = (Event) selectedEvent.getData();
        return (e.getVisibiltiy().equals(Visibility.PRIVATE));
    }
}
